<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib uri="http://www.bjs-tech.com/taglib/dataDict" prefix="dataDict" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>设置个性规则</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
    <link href="${basePath}/resources/zheng-admin/plugins/bootstrap-switch/bootstrap-switch.css" rel="stylesheet"/>
</head>
<body>
<div id="main">
    <form id="updateForm" method="post" class="form-horizontal" action="">
        <div id="updateDialog" class="container-fluid">
            <div class="row">

                <c:forEach var="config" items="${specialConfigs}">
                <div class="form-inline">
                    <label class="col-sm-4 control-label">${config.name}</label>&NonBreakingSpace;
                    <div class="col-sm-5" style="display: inline-block;">
                        <input type="checkbox" id="${config.code}_checked" name="${config.code}_checked">&NonBreakingSpace;
                        <input type="hidden"id="${config.code}_status" name="${config.code}_status">&NonBreakingSpace;
                        <c:if test="${config.isinput==1}">
                             <input type="text" class="form-control" id="${config.code}" name="${config.code}" value="" maxlength="25">&NonBreakingSpace;
                        </c:if>
                        <c:if test="${config.isinput==0}">
                            <input type="hidden" id="${config.code}" name="${config.code}" >&NonBreakingSpace;
                        </c:if>
                        <a class="lobibox-notify-info" href="javascript:;" data-toggle="tooltip" title="${config.description}"><i class="glyphicon glyphicon-info-sign"></i></a>
                    </div>
                </div>
                </c:forEach>

                <div class="form-inline">
                    <label class="col-sm-4 control-label">&nbsp;</label>
                    <div class="col-sm-5" style="display: inline-block;">&nbsp;</div>
                </div>
                    <div class="form-group">
                        <div class="col-sm-9 col-sm-offset-4">
                            <button type="button" class="btn btn-primary" onclick="createSubmit();">
                                保存
                            </button>
                        </div>
                    </div>
            </div>
        </div>
    </form>
</div>
</div>
</div>
<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<script src="${basePath}/resources/zheng-admin/plugins/bootstrap-switch/bootstrap-switch.js"></script>
<script>



    $(function(){
        /* 初始化控件 */
        <c:forEach var="config" items="${specialConfigs}">
            $("#${config.code}_checked").bootstrapSwitch({
                onText : "启用",      // 设置ON文本  
                offText : "禁用",    // 设置OFF文本  
                onColor : "success",// 设置ON文本颜色     (info/success/warning/danger/primary)  
                offColor : "default",  // 设置OFF文本颜色        (info/success/warning/danger/primary)  
                size : "normal",    // 设置控件大小,从小到大  (mini/small/normal/large)  
                // 当开关状态改变时触发  
                onSwitchChange : function(event, state) {
                    if (state == true) {
                        $("#${config.code}_status").val(1);
                    }else{
                        $("#${config.code}_status").val(0);
                    }
                }
            });
        </c:forEach>

        <c:forEach var="agentconfig" items="${agentConfigs}">
            if(1 == ${agentconfig.status}){
                $("#${agentconfig.code}_checked").bootstrapSwitch('state',true,true);
            }else{
                $("#${agentconfig.code}_checked").bootstrapSwitch('state',false,true);
            }
            $("#${agentconfig.code}_status").val(${agentconfig.status});
            $("#${agentconfig.code}").val(${agentconfig.value});
        </c:forEach>
    });

    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
            },
            messages: {
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({
                dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/specialconfigdetail/update',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                $.confirm({
                                    theme: 'dark',
                                    animation: 'rotateX',
                                    closeAnimation: 'rotateX',
                                    title: false,
                                    content: value.errorMsg,
                                    buttons: {
                                        confirm: {
                                            text: '确认',
                                            btnClass: 'waves-effect waves-button waves-light'
                                        }
                                    }
                                });
                            });
                        } else {
                            $.confirm({
                                theme: 'dark',
                                animation: 'rotateX',
                                closeAnimation: 'rotateX',
                                title: false,
                                content: result.data,
                                buttons: {
                                    confirm: {
                                        text: '确认',
                                        btnClass: 'waves-effect waves-button waves-light'
                                    }
                                }
                            });
                        }
                    } else {
                        window.document.location.reload();
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: textStatus,
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
            });
        }
    }
</script>
</body>
</html>